package com.tchepannou.limbe.gae.service;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.tchepannou.limbe.service.TaskQueueService;
import com.tchepannou.limbe.service.impl.AbstractService;

import java.util.Map;

/**
 * User: herve
 * Date: 14-06-18 1:20 AM
 */
public class GaeTaskQueueService extends AbstractService implements TaskQueueService
{
    @Override
    public void run(String queueName, String url, Map<String, String> params)
    {
        run(queueName, url, params, -1);
    }

    @Override
    public void run(String queueName, String url, Map<String, String> params, long delayMillis)
    {
        Queue queue = QueueFactory.getQueue(queueName);

        TaskOptions opts = TaskOptions.Builder.withUrl(url);
        if (delayMillis > 0)
        {
            opts.countdownMillis(delayMillis);
        }
        for (String name : params.keySet())
        {
            String value = params.get(name);
            if (value != null)
            {
                opts.param(name, value);
            }
        }

        queue.add(opts);
    }

}
